package run;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class ServerStarter {

	static String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	static String subPath = "src/main/webapp";
	static String etcPath = "etc/webdefault.xml";
	
	public static void main(String[] args) throws Exception {
		System.out.println("正在启动....................");
        long begin = System.currentTimeMillis();    
        Connector connector = new SelectChannelConnector();    
        connector.setPort(Integer.getInteger("jetty.port", 80).intValue());
        
        WebAppContext webapp = new WebAppContext(getPath()+subPath, "/");
        
        webapp.setDefaultsDescriptor(getPath()+etcPath);    
            
        Server server = new Server(); 
        server.setConnectors(new Connector[] { connector }); 
      
        server.setHandler(webapp);    
        server.start();    
        System.out.println("Jetty Server started, use " + (System.currentTimeMillis() - begin) + " ms, On path : "+getPath()+subPath);
    }
	
	private static String getPath(){
		if(path.indexOf("/") == 0){
			path = path.substring(1, path.length());
		}
		path = path.replace("target/test-classes/", "");
		return path;
	}

	
}
